<script setup>
import { ref, reactive, toRaw } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import Type from '@components/search/type';
import Search from '@components/search/index';
import PageWrapper from '@components/PageWrapper.vue';
import { common, service } from '@utils';
import { Plus, Edit, View } from '@element-plus/icons-vue';
import { usePremission, useRole } from '@hooks';
import { useRouter } from 'vue-router';

const router = useRouter();

const premission = usePremission({
    apply: 'bfqz:bill:apply',
});

const searchs = reactive([
    {
        type: Type.DATE_RANGE,
        width: 7,
    },
    {
        type: Type.SELECT,
        key: 'leaseMethod',
        width: 3,
        placeholder: '产品类型',
        valueKey: 'value',
        data: [],
    },
    {
        type: Type.INPUT,
        key: 'orderId',
        width: 5,
        placeholder: '订单编号',
    },
    {
        width: 6,
        type: Type.SEARCH_BUTTON,
    },
]);

const pageRef = ref();
const tableRef = ref();

let queryForm = {};

const onSearch = param => {
    queryForm = param;
    pageRef.value.refresh();
};

const getData = async ({ pageNum, pageSize }, cb) => {
    const param = { ...queryForm };
    if (param.time && param.time.length > 0) {
        // param.rentStartTime = param.time[0] + ' 00:00:00'
        // param.rentEndTime = param.time[1] + ' 23:59:59'
        param.rentStartTime = param.time[0];
        param.rentEndTime = param.time[1];
        delete param.time;
    }
    const res = await service.bfqz.billList(pageNum, pageSize, param);
    cb(res);
    setTimeout(() => {
        tableRef.value.scrollTo(0, 0);
    }, 0);
};

const exportData = async query => {
    query = { ...query };
    if (query.time && query.time.length > 0) {
        query.startCreateTime = query.time[0];
        query.endCreateTime = query.time[1];
        delete query.time;
    }
    query.fileName = '账单列表.xlsx';
    const res = await service.bfqz.billExport(query);
    res && common.export(res, query.fileName);
};

const getStatuss = async () => {
    const res = await service.auth.sysdirectListbyCode({ code: 'productType' });
    if (res) {
        let productType = [];
        res.forEach(item => {
            productType.push(item);
        });
        searchs[1].data = productType;
    }
};

getStatuss();
</script>
<template>
    <section class="main-white">
        <search @export="exportData" :items="searchs" @filter="onSearch" />
        <page-wrapper @get-data="getData" ref="pageRef">
            <template v-slot:default="props">
                <vxe-table class="mytable-scrollbar" ref="tableRef" :loading="props.loading" :data="props.tableData.records" :max-height="common.tableHeight(true, 1)" highlight-hover-row round stripe border>
                    <vxe-column type="seq" width="60"></vxe-column>
                    <vxe-column field="orderId" title="订单号" width="200" />
                    <vxe-column width="280" title="租期">
                        <template #default="{ row }">
                            <span>{{ row.rentDate }}</span>
                        </template>
                    </vxe-column>
                    <vxe-column title="产品类型">
                        <template #default="{ row }">{{ row.leaseMethod == 0 ? ' 先租后买' : ' 租完即送' }}</template>
                    </vxe-column>
                    <vxe-column field="stateDsc" title="订单状态" />
                    <vxe-column field="marketPrice" title="签约价" />
                    <vxe-column field="receivables" title="当前应收金额" />
                    <vxe-column field="actual" title="当前实际结算金额" />
                </vxe-table>
            </template>
        </page-wrapper>
    </section>
</template>
<style scoped lang="scss">
.card {
    width: 440px;
    height: 238px;
    border-radius: 15px;
    background-image: url(../../assets/image/card.png);
    background-size: 100% 100%;
    color: #fff;
    position: relative;
    span {
        position: absolute;
        font-family: PingFangSC-Regular, PingFang SC;
        font-size: 16px;
        height: 26px;
        line-height: 26px;
    }
    span:first-child {
        left: 44px;
        top: 21px;
        width: 80px;
        font-weight: 400;
    }
    span:nth-child(2) {
        left: 205px;
        top: 21px;
    }
    span:nth-child(3) {
        // width: 108px;
        left: 44px;
        top: 57px;
        height: 48px;
        font-size: 48px;
        font-family: DINCondensed-Bold, DINCondensed;
        font-weight: bold;
        line-height: 58px;
        letter-spacing: -2px;
    }
    span:nth-child(4) {
        left: 44px;
        top: 132px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        border-bottom: 1px solid #fff;
        cursor: pointer;
    }
    span:nth-child(5) {
        top: 132px;
        left: 235px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        border-bottom: 1px solid #fff;
        cursor: pointer;
    }
    span:nth-child(6) {
        top: 174px;
        left: 44px;
        width: 122px;
        height: 38px;
        line-height: 38px;
        background: #fff;
        color: #d22f1b;
        text-align: center;
        border-radius: 2px;
        cursor: pointer;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
    }
}
</style>
